import { singleton } from "tsyringe";
import { RobotAppPage } from "../../page";
import { sendToWebsocket } from "../../../utils/send";
import { getDeviceUuid } from "../../../tokens";
import { $ } from "../../../utils";

@singleton()
export class KwaiShouHomeActivity extends RobotAppPage {
    title: string = `首页`
    private isRuning: boolean = false;
    dispose(): void {
        this.out()
    }
    is(packageName: string): boolean {
        return packageName === `com.smile.gifmaker` || `com.kuaishou.nebula` === packageName
    }
    accept(activityName: string): boolean {
        return `com.yxcorp.gifshow.HomeActivity` === activityName;
    }
    in(): boolean {
        if (this.isRuning) return;
        this.isRuning = true;
        /**
         * 发送 消息给服务器
         */
        const node = selector().textMatches('我知道了|朋友推荐').visibleToUser(true).findOne()
        const dialogText = node?.text()
        if (dialogText === '我知道了') {
            $.clickButton(node)
        }
        else if(dialogText === '朋友推荐'){
            back()
            sleep(500)
        }
        sendToWebsocket({ type: "HOME_ACTIVITY", payload: { uuid: getDeviceUuid() } })
        log(`KwaiShouHomeActivity In: 从服务器获取任务信息`)
        return true;
    }
    out(): boolean {
        if (!this.isRuning) return false;
        this.isRuning = false;
        log(`KwaiShouHomeActivity Out`)
        return true;
    }
}
